<?php
namespace App\Http\Controllers\Admin;

use App\Models\Banners;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
class BannersController extends BaseController
{

    /**
     * 获取列表  首页轮播
     */
    public function index()
    {
        if(!$this->user->can('viewContentBanner'))
            abort(405);
        
        $result = Banners::orderBy('sort',' asc')->orderBy('created_at','desc')->paginate(20);
        return view('admin.banners.list')->with('result',$result);
    }
    
    /**
     * banner 添加
     */
    public function create()
    {
        if(!$this->user->can('editContentBanner'))
            abort(405);
        
        $viewPath = "admin.banners.add";

        return view($viewPath)->with('blademode','add');
    }

    /**
     * banner 添加
     */
    public function edit($bannerid)
    {
        if(!$this->user->can('viewContentBanner'))
            abort(405);
        
        $banners = Banners::where('id','=',$bannerid)->first();
        return view('admin.banners.details')->with('blademode','edit')
                ->with('banners',$banners);
    }

    /**
     * banner 保存
     */
    public function store(Request $req)
    {
        if(!$this->user->can('editContentBanner'))
            abort(405);
        
        $data = $req->only('title','description','aid','sort','app','bannerid','link');
        if($data['bannerid'] > 0 && !empty($data['bannerid']))
        {
            $banners = Banners::find($data['bannerid']);
        }
        else
        {
            $banners = new Banners();
        }
        $banners->title = $data['title'];
        $banners->description = $data['description'];
        $banners->aid = $data['aid'];
        $banners->sort = $data['sort'];
        $banners->app = $data['app'];
        $banners->link = $data['link'];
        $banners->save();
        return redirect()->intended('admin/content/banner');
    }
    
    /**
     * 删除
     */
    public function destroy($id)
    {
        if(!$this->user->can('deleteContentBanner'))
            return $this->formatJson('error','抱歉，您没有相关操作权限');
        
        $banner = Banners::find($id);
        if(!empty($banner))
        {
            $banner->delete();
            return  Response::json(array('status'=>'success','msg'=>'轮播图：［'.$banner->title.'］ 删除成功'));
        }
    }
}